package com.ctgu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ctgu.common.ResponseResult;
import com.ctgu.mapper.EmployeeMapper;
import com.ctgu.model.entity.Employee;
import com.ctgu.model.entity.Position;
import com.ctgu.mapper.PositionMapper;
import com.ctgu.service.IEmployeeService;
import com.ctgu.service.IPositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiMa
 * @since 2023-07-05
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {

    @Autowired
    @Lazy
    private EmployeeServiceImpl employeeService;

    @Override
    public ResponseResult getList(Integer depId) {
        LambdaQueryWrapper<Position> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Position::getDepId,depId);
        List<Position> list = list(wrapper);
        return new ResponseResult<>(200,"查询成功",list);
    }

    @Override
    public ResponseResult updatePosition(Integer[] eId, Integer posId) {
        LambdaUpdateWrapper<Employee> wrapper=new LambdaUpdateWrapper<>();
        Position byId = getById(posId);
        wrapper.in(Employee::getId,eId);
        if(byId.getFlag().equals(1)){
            wrapper.set(Employee::getRoleId,"2");
        }
        wrapper.set(Employee::getPosId,posId);
        employeeService.update(wrapper);
        return new ResponseResult<>(200,"转移成功");
    }

    @Override
    public ResponseResult add(Position position) {
        if(name(position.getDepId(),position.getName())){
            return new ResponseResult<>(400,"该职位名已存在");
        }
        position.setCreateDate(LocalDateTime.now());
        save(position);
        return new ResponseResult<>(200,"保存成功");
    }

    @Override
    public ResponseResult delete(Integer id) {
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getPosId,id);
        List<Employee> list =employeeService.list(wrapper);
        if(list.size()!=0){
            return new ResponseResult<>(400,"该部门下还有员工请移除后在删除");
        }
        this.removeById(id);
        return new ResponseResult<>(200,"删除成功");
    }

    @Override
    public ResponseResult updateInfo(Integer id, String name) {
        if(name(id,name)){
            return new ResponseResult<>(400,"该职位名已存在");
        }
        Position byId = getById(id);
        byId.setName(name);
        updateById(byId);
        return new ResponseResult<>(200,"更新成功");
    }

    public boolean name(Integer depId,String name){
        LambdaQueryWrapper<Position> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(Position::getDepId,depId);
        wrapper.eq(Position::getName,name);
        Position one = getOne(wrapper);
        if(one==null){
            return false;
        }
        return true;
    }
}
